package com.house.business.service.impl;

import com.google.common.collect.Lists;
import com.house.business.entity.LeasingInformation;
import com.house.business.vo.LeasingInformationVo;
import com.house.business.mapper.LeasingInformationMapper;
import com.house.business.service.LeasingInformationService;
import com.house.framework.entity.AjaxResult;
import com.house.framework.service.impl.ServiceBaseImpl;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.house.framework.annotations.Slave;
import com.house.common.IdBuilder;

import java.util.Objects;

/**
 * <p>
 * 租赁信息表 服务实现类
 * </p>
 *
 * @since 2023-05-09
 */
@Service
public class LeasingInformationServiceImpl extends ServiceBaseImpl<LeasingInformationMapper, LeasingInformation> implements LeasingInformationService {

    @Autowired
    private LeasingInformationMapper leasingInformationMapper;

    /**
    * 新增租赁信息表
    * @param entity
    * t_leasing_information新增数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object addLeasingInformation(LeasingInformation entity){

        leasingInformationMapper.insert(entity);
        return AjaxResult.success();
    }

    /**
    * 修改租赁信息表
    * @param entity
    * t_leasing_information修改数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object updateLeasingInformation(LeasingInformation entity){
        leasingInformationMapper.updateById(entity);
        return AjaxResult.success();
    }

    /**
    * 取指定ID的租赁信息表数据
    * @param id
    * t_leasing_information 主键ID
    * @return LeasingInformationVo
    * 返回指定ID的租赁信息表数据
    */
    @Slave
    @Override
    public LeasingInformationVo getLeasingInformationById(Long id) {
        return leasingInformationMapper.get(id);
    }

    /**
    * 租赁信息表分页取数
    * @param entityVo
    * t_leasing_information 分页取数参数，pageIndex=页码；pageSize=每页记录数；
    * @return
    * 返回分页数据
    */
    @Slave
    @Override
    public IPage<LeasingInformationVo> getLeasingInformationList(LeasingInformationVo entityVo) {
        int pageIndex = entityVo.getPageIndex();
        int pageSize = entityVo.getPageSize();
        IPage page = new Page<LeasingInformationVo>(pageIndex, pageSize);
        QueryWrapper<LeasingInformationVo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotEmpty(entityVo.getTenantName()), "tenant_name", entityVo.getTenantName())
                .eq(Objects.nonNull(entityVo.getLeaseState()), "lease_state", entityVo.getLeaseState())
                .orderByDesc(Lists.newArrayList("paid_in_time", "payable_time"));
        return leasingInformationMapper.page(page, queryWrapper);
    }

    /**
    * 删除指定ID的租赁信息表数据
    * @param id
    * t_leasing_information 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteLeasingInformationById(Long id){
        leasingInformationMapper.delete(id);
    }

    /**
    * 逻辑删除指定ID的租赁信息表数据
    * @param id
    * t_leasing_information 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void logicDeleteById(Long id){
        this.logicDelete(id, LeasingInformationMapper.class, LeasingInformation.class);
    }


    /**
    * 批量删除指定ID的租赁信息表数据
    * @param ids
    * t_leasing_information 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchDelete(Long[] ids){
        leasingInformationMapper.batchDelete(ids);
    }

    /**
    * 批量逻辑删除指定ID的租赁信息表数据
    * @param ids
    * t_leasing_information 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchLogicDelete(Long[] ids){
        for(Long id : ids){
            this.logicDelete(id, LeasingInformationMapper.class, LeasingInformation.class);
        }
    }
}
